<template>
	<view>
		<view class="toptitle">
			<image mode="widthFix"
				src="https://phantombody.oss-cn-shanghai.aliyuncs.com/eb326fdbe1d408db023418c32529a75457961062a9c0cb710a3ab7aba73f41c4.png">
			</image>
			<view>个人中心</view>
		</view>
		<u-popup :show="nshow" :round="10" mode="center" @close="close" @open="open">
			<view class="upopBox">
				<view class="utitle">修改昵称</view>
				<input class="uinp" @input="CKnickname" :value="new_name" />
				<view class="ubtn" @click="updataName()">修改</view>
			</view>
		</u-popup>
		<u-popup :show="nshow2" :round="10" mode="center" @close="close2" @open="open2">
			<view class="upopBox2">
				<view class="utitle">{{userData.nickname}}</view>
				<image class="scan_img" v-if="nshow2"  :src="_QrCode" mode="widthFix"></image>
			</view>
		</u-popup>
		<view class="avaBox">
			<u-row customStyle="margin: 25rpx  25rpx 0" gutter="10">
				<u-col span="2">
					<image @click="upimg()" class="avarimg" :src="userData.avatar || 'https://phantombody.oss-cn-shanghai.aliyuncs.com/Role/Picture/role_default_0.jpg'
					"></image>
				</u-col>
				<u-col span="10">
					<view class="u-marle-left name">
						<u-row gutter="10">
							<u-col span="9">
								昵称：{{userData.nickname||'请先登录'}}
							</u-col>
							<u-col span="3">
								<u-icon name="edit-pen" color="#fff" size="28" @click="shownickname"></u-icon>
							</u-col>
						</u-row>

					</view>
					<view class="u-marle-left name">
						<u-row gutter="10">
							<u-col span="9">
								ID:{{userData.id||'0'}}
							</u-col>
							<u-col span="3">
								
								<image style="width: 40rpx;height: 40rpx;" src="../../static/icon_qr_code.png"  @click="shownickname2"></image>
								<!-- <u-icon name="scan" color="#fff" size="28" @click="shownickname2"></u-icon> -->
							</u-col>
						</u-row>
					</view>
					<u-row gutter="10">
						<u-col span="8">
							<view class="u-marle-left id">积分：{{userData.integral ||'0'}}</view>
						</u-col>
						<u-col span="4">
							<view class="u-marle-right id">
								<view @click="goto('/pages/mine/integralBuy')" class="jifenbtn">获取积分</view>
							</view>
						</u-col>
					</u-row>
				</u-col>
			</u-row>

		</view>
		<view class="addrBox">
			<u-row customStyle="margin:20rpx 25rpx" gutter="10">
				<u-col span="10">
					收货姓名：{{(addrlist && addrlist.name)||'暂无收货姓名'}}
				</u-col>
				<u-col span="2">
					<navigator url="/pages/mine/addressList">
						<u-icon name="edit-pen" color="#fff" size="28"></u-icon>
					</navigator>
				</u-col>
			</u-row>
			<u-row customStyle="margin:20rpx 25rpx" gutter="10">
				<u-col span="12">
					收货电话：{{(addrlist && addrlist.mobile)||'暂无收货电话'}}
				</u-col>
			</u-row>
			<u-row customStyle="margin:20rpx 25rpx" gutter="10">
				<u-col span="12">
					收货地址：{{(addrlist && addrlist.areaName)||'暂无收货地址'}}
				</u-col>
			</u-row>
		</view>
		<view class="mineTitle">空间</view>
		<view class="listBox">
			<view class="listItem" v-for="(item,index) in goodsList.slice(0,7)" :key="index"
				@click="ckimg(item.videoUrl,2)">
				<image mode="aspectFill" :src="item.picUrl"></image>
			</view>
			<view class="listItem2" @click="mores(0)">
				<u-icon labelPos="center" top="15" name="more-dot-fill" color="#fff" size="30"></u-icon>
			</view>
		</view>
		<!-- <view class="mineTitle">照片</view>
		<view class="listBox">
			<view class="listItem" v-for="(item,index) in imglist.slice(0,7)" :key="index" @click="ckimg2(item.url,1)">
				<image mode="aspectFill" :src="item.url || item.picUrl"></image>
			</view>
			<view class="listItem2" @click="mores(1)" v-if="imglist.length>=7">
				<u-icon labelPos="center" top="15" name="more-dot-fill" color="#fff" size="30"></u-icon>
			</view>
		</view>
		<view class="mineTitle">视频</view>
		<view class="listBox">
			<view class="listItem" v-for="(item,index) in videolist.slice(0,7)" :key="index"
				@click="ckimg2(item.videoUrl,2)">
				<image mode="aspectFill" :src=" item.picUrl"></image>
			</view>
			<view class="listItem2" @click="mores(2)" v-if="videolist.length>=7">
				<u-icon labelPos="center" top="15" name="more-dot-fill" color="#fff" size="30"></u-icon>
			</view>
		</view> -->
		<view class="tabbarBoxBG"></view>
		<image class="tabItem1" @click="navclick(1)" src="../../static/tab1.png" mode="widthFix"></image>
		<image class="tabItem2" @click="scanCode()" src="../../static/tab0.png" mode="widthFix"></image>
		<image class="tabItem3" src="../../static/tab2.png" mode="widthFix"></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// src: 'https://ps.ssl.qhimg.com/t011a210d2289a9d0c8.jpg',
				userData: [],
				goodsList: [],
				videolist: [],
				imglist: [],
				addrlist: [],
				Uid: '',
				nshow: false,
				nshow2: false,
				new_name: '',
				userPassQrUrl: '',
				_QrCode:''
			}
		},
		async onLoad() {
			
			var accessToken = uni.getStorageSync('refreshToken');
			
			this.userPassQrUrl = "https://api.huanti3d.com/app-api/system/user-share-qr/get?content="+accessToken
			let data = await this.getImageInfo({imgSrc:this.userPassQrUrl})
			this._QrCode = data.path
			
			console.log('_QrCode',this._QrCode)
			console.log('image',data)
			
		},
		onShow() {
			if (!uni.getStorageSync('accessToken')) {
				uni.navigateTo({
					url: '/pages/login/index'
				})
			} else {
				this.Uid = uni.getStorageSync('userId');
				this.API_user();
				this.API_Shop();
				this.API_img()
				this.API_viedo()
				this.API_addr()
			}
		},

		methods: {
			async getImageInfo({imgSrc}) {
				console.log('imgSrc',imgSrc)
				return new Promise((resolve, errs) => {
					uni.getImageInfo({
						src: imgSrc,
						success: function(image) { 
							resolve(image);
						},
						fail(err) {
							errs(err);  
						}
					});
				});
			},
			CKnickname(e) {
				this.new_name = e.detail.value;
				console.log(this.new_name)
			},
			updataName() {
				var that = this;
				that.ran
					.request('/app-api/member/user/update-nickname?nickname=' + that.new_name, {}, 'PUT')
					.then(res => {
						if (res.code == 200) {
							that.API_user();
							that.nshow = false;
						} else {
							uni.showToast({
								title: res.msg,
								duration: 2000
							});
						}
					});
			},
			navclick(id) {
				uni.reLaunch({
					url: '/pages/3d/index'
				})

			},
			open() {
				console.log('open');
				this.nshow = true;
			},
			close() {
				this.nshow = false
				// console.log('close');
			},
			open2() {
				console.log('open');
				this.nshow2 = true;
			},
			close2() {
				this.nshow2 = false
				// console.log('close');
			},
			shownickname() {
				this.nshow = true;
			},
			shownickname2() {
				this.nshow2 = true;
			},
			upimg() {
				var that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						that.API_updown(res.tempFilePaths[0])
					}
				});
			},
			API_updown(filePath) {
				var that = this;
				var accessToken = uni.getStorageSync('accessToken');
				uni.uploadFile({
					url: 'https://api.huanti3d.com/app-api/member/user/update-avatar', //仅为示例，非真实的接口地址
					filePath: filePath,
					name: 'avatarFile',
					header: {
						"tenant-id": 1,
						"Authorization": "Bearer " + accessToken
					},
					success: (uploadFileRes) => {
						that.API_user()
					}
				});

			},
			scanCode() {
				let that = this
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						// {"store_id":3,"dev_sn":"10000000668df2e3","type":1}
						var data = JSON.parse(res.result)
						if (data.type == 1) {
							// 创建订单
							uni.navigateTo({
								url: '/pages/mine/reserve?storeId=' + data.store_id,
							})

						} else if (data.type == 2) {
							// data.id
							var orderId = data.id
							// 
							that.ran
								.request('/app-api/system/user-model-order/bind', {
									"id": orderId
								}, 'PUT')
								.then(res => {
									console.log('getStoreInfo orderId', res);
									if (res.code == 200) {
										// 表示绑定成功
										uni.navigateTo({
											url: '/pages/mine/reserve?storeId=' + res.data
												.storeId + "&orderId=" + orderId,
										})
									} else {
										uni.showToast({
											title: res.msg,
											duration: 2000
										});
									}
								});
							// 绑定订单

						}
					}
				})

			},
			API_addr() {
				var that = this;
				that.ran
					.request('/app-api/member/address/list', {}, 'GET')
					.then(res => {
						console.log(res);
						if (res.code == 200) {
							if (res.data.lenth != 0) {
								that.addrlist = res.data[0];
							}
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					});
			},
			login() {
				uni.navigateTo({
					url: '/pages/login/index'
				})
			},
			mores(type) {
				if (type == 0) {
					uni.navigateTo({
						url: '/pages/index/index'
					})
				} else {
					uni.navigateTo({
						url: '/pages/mine/huantiMore?type=' + type
					})
				}

			},
			goto(url) {
				uni.navigateTo({
					url: url
				})
			},
			ckimg(url, type) {
				uni.navigateTo({
					url: '/pages/mine/viedo?type=' + type + '&srcs=' + url
				})
			},
			ckimg2(url, type) {
				uni.navigateTo({
					url: '/pages/mine/viedo?type=' + type + '&srcs=' + url
				})
			},
			API_user() {
				var that = this;
				that.ran
					.request('/app-api/member/user/get', {}, 'GET')
					.then(res => {
						console.log(res);
						if (res.code == 200) {
							that.userData = res.data;
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					});
			},
			API_Shop() {
				var that = this;
				var url = '/app-api/product/spu/page-buy?pageNo=1' + "&pageSize=7&brandId=1" +
					"&buyState=-1"
				that.ran
					.request(url, {}, 'GET')
					.then(res => {
						console.log(res);
						if (res.code == 200) {

							that.goodsList = that.goodsList.concat(res.data.list);
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					});
			},
			API_img() {
				var that = this;
				that.ran
					.request('/app-api/system/user-model-media/page?pageNo=1&pageSize=7&type=IMAGE', {}, 'GET')
					.then(res => {
						console.log(res);
						if (res.code == 200) {
							that.imglist = res.data.list;
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					});
			},
			API_viedo() {
				var that = this;
				that.ran
					.request('/app-api/system/user-model-media/page?pageNo=1&pageSize=7&type=VIDEO', {}, 'GET')
					.then(res => {
						console.log(res);
						if (res.code == 200) {
							res.data.list.map(item => {
								if (!item.picUrl) {
									item.picUrl = item.url + "?x-oss-process=video/snapshot,t_4000,m_fast"
								}
							})
							that.videolist = res.data.list;

						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: url('https://phantombody.oss-cn-shanghai.aliyuncs.com/05efac942e35d4f935dbd6c9a17673707bcb9b2dbd8fe230f691434d7f3b56d4.jpg');
		background-repeat: no-repeat;
		background-size: 100vw 180vh;
	}

	.upopBox2 {
		padding: 30rpx;

		.scan_img {
			width: 400rpx;
			height: 400rpx;
			display: block;
			padding: 20rpx 0;
		}

		.utitle {
			text-align: center;
			line-height: 2;
		}
	}

	.upopBox {
		padding: 30rpx;

		.utitle {
			text-align: center;
			padding-bottom: 40rpx;
		}

		.uinp {
			border-radius: 40rpx;
			padding: 0 10rpx;
			height: 60rpx;
			line-height: 60rpx;
			border: 1px solid #46526B;

		}

		.ubtn {
			background: #836FFE;
			height: 60rpx;
			line-height: 60rpx;
			border-radius: 40rpx;
			text-align: center;
			color: #FFF;
			margin-top: 40rpx;
		}
	}

	.textr {
		text-align: right;
		padding: 0 20rpx;
	}

	.u-marle-left {
		margin-left: 40rpx;
	}

	.mineTitle {
		line-height: 80rpx;
		margin: 10rpx 60rpx;
		padding: 0 20rpx;
		background: #46526B;
		border-radius: 10rpx;
		color: #fff;
	}

	.listBox {
		overflow: hidden;
		margin: 5rpx 0 0 60rpx;
		padding: 5rpx 0 5rpx 20rpx;

		.listItem2 {
			width: 130rpx;
			height: 130rpx;
			margin: 20rpx 20rpx 20rpx 0;
			background: #46526B;
			opacity: 0.5;
			box-sizing: border-box;
			border: 1px solid #B8B8B8;
			float: left;
			border-radius: 14rpx;
			text-align: center;
			line-height: 130rpx;
		}

		.listItem {
			width: 130rpx;
			margin: 20rpx 20rpx 20rpx 0;
			float: left;

			image {
				width: 130rpx;
				height: 130rpx;
				display: block;
				border-radius: 12rpx;
			}

			.goods_name {
				padding: 0 10rpx;
			}
		}
	}

	.addrBox {
		margin: 50rpx 60rpx;
		padding: 5rpx 0;
		background: rgba(255, 255, 255, 0.2);
		border-radius: 20rpx;
		color: #fff;
		font-size: 30rpx;
	}

	.toptitle {
		width: 750rpx;
		position: relative;
		margin: 100rpx 0 20rpx;

		image {
			width: 250rpx;
			display: block;
			margin: 0 250rpx;
			position: absolute;
		}

		view {
			width: 750rpx;
			text-align: center;
			color: #fff;
			position: absolute;
			z-index: 2;
			top: 10rpx;
		}
	}

	.bgs {
		position: absolute;
		top: 0;
		left: 0;
		z-index: -1;
		width: 100vw;
		height: 200vh;
	}

	.linebox {
		// width: 660rpx;
		margin: 50rpx auto;
	}

	.cardBox {
		width: 660rpx;
		margin: 50rpx auto;
		padding: 25rpx 20rpx;
		border-radius: 24rpx;
		background-image: linear-gradient(to bottom right, #B8B8B8, #D8D8D8);

		.number {
			font-weight: bold;
			line-height: 50rpx;
			font-size: 36rpx;

			text {
				font-weight: normal;
				padding-left: 20rpx;
			}
		}
	}


	.avaBox {
		color: #fff;
		padding-top: 100rpx;

		.malt {
			margin-left: 100rpx;
		}

		.avarimg {
			margin-left: 20rpx;
			width: 100rpx;
			height: 100rpx;
			border-radius: 70rpx;
			display: block;
		}

		.jifenbtn {
			background: rgba(255, 255, 255, 0.5);
			border-radius: 20rpx;
			text-align: center;
			margin: 0 40rpx 0 0;
			padding: 0 10rpx;
			font-size: 24rpx;
		}

		.name {
			line-height: 60rpx;
			font-size: 34rpx;
		}

		.id {
			line-height: 60rpx;
			font-size: 34rpx;
		}
	}
</style>